package com.example.childrendapp.database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;

public class DataBaseHelper extends SQLiteOpenHelper {

	// The Android's default system path of your application database.
	private static final String DB_PATH = "/data/data/com.example.childrendapp/databases/";

	private static final String DB_NAME = "Manager";
	

	private SQLiteDatabase myDataBase;

	private final Context myContext;
	public static final String COLUMN_ID1 = "rowid";
	public static final String COLUMN_NAME = "UserName";
	public static final String COLUMN_PASS = "PassWord";
	public static final String COLUMN_ID2 = "rowid";
	public static final String COLUMN_QUESTION = "Question";
	public static final String COLUMN_ANSWER1 = "Answer1";
	public static final String COLUMN_ANSWER2 = "Answer2";
	public static final String COLUMN_ANSWER3 = "Answer3";
	public static final String COLUMN_ANSWER4 = "Answer4";
	public static final String COLUMN_RESULT = "Result";
	public static final String TABLE1 = "USER";
	public static final String TABLE2 = "MATHS";
	public DataBaseHelper(Context context) {

		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}

	/**
	 * Creates a empty database on the system and rewrites it with your own
	 * database.
	 * */
	public void createDataBase() throws IOException {

		boolean dbExist = checkDataBase();

		if (dbExist) {
		} 
		else {
			this.getReadableDatabase();

			try {

				copyDataBase();

			} catch (IOException e) {

				throw new Error("Error copying database");

			}
		}

	}
	private boolean checkDataBase() {

		SQLiteDatabase checkDB = null;

		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READONLY);

		} catch (SQLiteException e) {

			// database does't exist yet.

		}

		if (checkDB != null) {

			checkDB.close();

		}

		return checkDB != null ? true : false;
	}

	public void copyDataBase() throws IOException {

		InputStream myInput = myContext.getAssets().open("Manager.sqlite");

		String outFileName = DB_PATH + DB_NAME;

		OutputStream myOutput = new FileOutputStream(outFileName);

		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		myOutput.flush();
		myOutput.close();
		myInput.close();

	}

	public void openDataBase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READONLY);

	}

	@Override
	public synchronized void close() {

		if (myDataBase != null)
			myDataBase.close();
		super.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	


  /*  public Cursor getCursor() {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables("USER");
        String[] asColumnsToReturn1 = new String[] {COLUMN_ID1,COLUMN_NAME,
                COLUMN_PASS };
        queryBuilder.setTables("MATHS");
        String[] asColumnsToReturn2 = new String[] {COLUMN_ID2,COLUMN_QUESTION,COLUMN_ANSWER1,COLUMN_ANSWER2,COLUMN_ANSWER3,COLUMN_ANSWER4,
        		COLUMN_RESULT };
        Cursor mCursor1 = queryBuilder.query(myDataBase, asColumnsToReturn1, null,
                null, null, null,null);
        Cursor mCursor2 = queryBuilder.query(myDataBase, asColumnsToReturn2, null,
                null, null, null,null);
        return mCursor2;
    }*/
   

    public void Add(String name,String pass){
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues cv=new ContentValues();
		cv.put("UserName", name);
		cv.put("PassWord", pass);
		db.insert("USER", null, cv);
		db.close();
	}
	public String[][] getAll(String query){
		String [][] array=null;
		SQLiteDatabase db=null;
		Cursor cursor=null;
		try {
			db=this.getReadableDatabase();
			cursor=db.rawQuery(query, null);
			if(!cursor.isFirst()){
				cursor.moveToFirst();
			}
			int rowCount=cursor.getCount();
			int columnCount=cursor.getColumnCount();
			array=new String[rowCount][columnCount];
			int index=0;
			if(cursor.isFirst()){
				do {
					for(int j=0;j<columnCount;j++){
						array[index][j]=cursor.getString(j);
						System.out.println(cursor.getColumnName(j)+":"+cursor.getString(j));
						System.out.println();
						
					}
					index++;
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(cursor!=null){
				cursor.close();
			}
			if(db!=null){
				db.close();
			}
		}
		
		return array;
	}
   
	

}
